package bb;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Component;

import bb.action.CourseAction;
import bb.action.ProductAction;
import bb.action.UserAction;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.StaticHandler;
import io.vertx.ext.web.templ.HandlebarsTemplateEngine;

@Component
public class VertxSpringTest extends AbstractVerticle {

	@Resource
	private DataSource dataSource;

	@Override
	public void start() throws Exception {

		Router router = Router.router(vertx);
		router.route("/*.html").handler(StaticHandler.create());// 开启静态资源
		// 开启jdbc
		router.route("/*").handler(ctx -> {

			Connection sqlconn = null;
			try {
				sqlconn = dataSource.getConnection();
			} catch (Exception e) {
				e.printStackTrace();
			}
			ctx.put("conn", sqlconn);// 加入到ctx

			// In order to use a template we first need to create an engine
			final HandlebarsTemplateEngine engine = HandlebarsTemplateEngine.create();
			ctx.put("engine", engine);

			ctx.next();
		});

		router.get("/products").handler(this::print2);
		router.get("/page").handler(this::print3);
		router.get("/queryuser").handler(this::queryuser);
		Handler n = new Handler() {
			@Override
			public void handle(Object event) {
			}
		};
		router.get("/hbs").handler(n);
		
		UserAction ua = context.getBean(UserAction.class);
		router.get("/login").handler(ua::login);
		
		ProductAction pa = context.getBean(ProductAction.class);
		router.get("/queryProduct").handler(pa::queryProduct);
		
		
		CourseAction ca = context.getBean(CourseAction.class);
		router.get("/queryCourse").handler(ca::queryCourse);
		
		router.get("/*").handler(this::print1);

		// 传递方法引用，监听端口
		vertx.createHttpServer().requestHandler(router::accept).listen(8080);

		/*
		 * vertx.createHttpServer().requestHandler(req -> {
		 * if(req.path().equals("/index")){
		 * req.response().sendFile("index.html"); } }).listen(8080);
		 */
	}

	public void print1(RoutingContext ctx) {
		ctx.response().putHeader("content-type", "text/html").end("Hello World");
	}

	public void print2(RoutingContext ctx) {
		ctx.response().putHeader("content-type", "text/html").end("Hi products");
	}

	public void print3(RoutingContext ctx) {
		ctx.response().sendFile("index.html");

	}

	public void queryuser(RoutingContext ctx) {

		StringBuffer sb = new StringBuffer();

		Connection connection = ctx.get("conn");
		Statement stmt;
		try {
			stmt = connection.createStatement();
			ResultSet rs = stmt.executeQuery("select *from user");
			while (rs.next()) {
				sb.append(rs.getObject(1)).append(",");
				sb.append(rs.getObject(2)).append(",");
				sb.append(rs.getObject(3));
				ctx.response().putHeader("content-type", "text/html;charset=UTF-8").end(sb.toString());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	public void hbs(RoutingContext ctx) {

		ctx.put("name", "hbs 渲染");

		// and now delegate to the engine to render it.
		((HandlebarsTemplateEngine) ctx.get("engine")).render(ctx, "templates/index.hbs", res -> {
			if (res.succeeded()) {
				ctx.response().end(res.result());
			} else {
				ctx.fail(res.cause());
			}
		});
	}

	
	private static ApplicationContext context; 
	public static void main(String[] args) {

		context = new AnnotationConfigApplicationContext(SpringConfiguration.class);

		System.setProperty("vertx.cwd", "E:\\work\\atest\\atest\\src\\main\\java\\bb");// 路径前缀

		Vertx vertx = Vertx.vertx(new VertxOptions().setClustered(false));
		vertx.deployVerticle(context.getBean(VertxSpringTest.class));
	}

}
